package com.huluobo.a2203kotlinjetpackdemo.livedata

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import com.huluobo.a2203kotlinjetpackdemo.R

class StudentActivity : AppCompatActivity() {
    private lateinit var studentViewModel: StudentViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_student)
        val studentTv = findViewById<TextView>(R.id.student_tv)

        studentViewModel = ViewModelProvider(this)[StudentViewModel::class.java]

        //创建了一个学生对象,并且赋值
        val student = Student("LC", "0", 90)
        studentViewModel.setStudentInfo(student)

        //观察viewModel中livedata的数据变化,根据数据变化赋值
        studentViewModel._student.observe(this) {
            studentTv.text = "分数:$it"
        }
    }
}